package com.xdw.addressbookbyjava.provider;

import com.xdw.addressbookbyjava.ResourceTable;
import com.xdw.addressbookbyjava.customview.RoundImage;
import com.xdw.addressbookbyjava.model.Contacts;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

/**
 * Created by 夏德旺 on 2020/12/31 20:58
 */
public class ContactsProvider extends RecycleItemProvider {
    private List<Contacts> list;   //数据源
    private AbilitySlice slice;    //context对象

    /**
     * 构造函数
     *
     * @param list
     * @param slice
     */
    public ContactsProvider(List<Contacts> list, AbilitySlice slice) {
        this.list = list;
        this.slice = slice;
    }

    /**
     * 数据源的大小
     *
     * @return
     */
    @Override
    public int getCount() {
        return list.size();
    }

    /**
     * 通过索引获取item
     *
     * @param position
     * @return
     */
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    /**
     * 获取索引
     *
     * @param position
     * @return
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 加载填充每一个Item内部的控件的数据
     *
     * @param position
     * @param component
     * @param componentContainer
     * @return
     */
    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        Component cpt = component;
        if (cpt == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_item_contacts, null, false);
        }
        Contacts contacts = list.get(position);
        Text textName = (Text) cpt.findComponentById(ResourceTable.Id_item_name);
        RoundImage imageGenderIcon = (RoundImage) cpt.findComponentById(ResourceTable.Id_item_gender_icon);
        textName.setText(contacts.getName());
        if (contacts.getGender() == 0) {
            imageGenderIcon.setPixelMapAndCircle(ResourceTable.Media_man);
        } else {
            imageGenderIcon.setPixelMapAndCircle(ResourceTable.Media_lady);
        }
        return cpt;
    }
}
